package com.jia.leetCode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @program: Leetcode
 * @description:  207. Course Schedule
 * @author: STU756
 * @create: 2020-08-17 11:01
 */
public class Pro0207 {
    //
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        List<Integer>[] adjacency = new ArrayList[numCourses];
        for(int i = 0; i < numCourses; i++) {
            adjacency[i] = new ArrayList<>();
        }
        //入度
        int[] indegree = new int[numCourses];
        for(int[] p : prerequisites) {
            adjacency[p[1]].add(p[0]);
            indegree[p[0]]++;
        }
        Queue<Integer> queue = new LinkedList<>();
        for(int i = 0; i < numCourses; i++) {
            if(indegree[i] == 0) {
                queue.offer(i);
            }
        }
        while(!queue.isEmpty()) {
            int cur = queue.poll();
            --numCourses;
            for(Integer next : adjacency[cur]) {
                if(--indegree[next] == 0) {
                    queue.offer(next);
                }
            }
        }
        return numCourses == 0;
    }
}
